(define-module (sudoku utils))

(use-modules (srfi srfi-1)
             (ice-9 receive)
             (ice-9 control)
             (sudoku square))

(define-public (string->num-list s)
  (map (lambda (char)
         (string->number (string char)))
       (string->list s)))

(define-public (rank1->rank2 l)
  (letrec ((n (inexact->exact (expt (length l)
                                    (/ 1 2))))
           (loop (lambda (r1l r2l)
                   (if (null? r1l)
                       r2l
                       (loop (drop r1l n)
                             (cons (take r1l n) r2l))))))
    (reverse (loop l '()))))

(define-public (array-fold proc acc array)
  (do ((i 0 (1+ i)))
      ((>= i (array-length array)) acc)
    (do ((j 0 (1+ j)))
        ((>= j (array-length array)))
      (set! acc (proc (list (array-ref array i j) i j) acc)))))

(define-public (display-board board)
  (let ((v-board (make-array 0 9 9)))
    (newline)
    (array-map! v-board get-value board)
    (map (lambda (row)
           (display row) (newline))
         (array->list v-board))))
